package assg.funnycells.util;

import java.io.PrintStream;
import java.util.Calendar;

public class Logger {
    public final static int OFF = 0;
    public final static int INFO = 1;
    public final static int DEBUG = 2;

    static Logger logger = null;

    int logLevel;
    PrintStream out;
    Calendar calendar;
    StringBuilder strbuilder;

    public Logger() {
	logLevel = INFO;
	out = System.out;
	calendar = Calendar.getInstance();
	strbuilder = new StringBuilder();
    }

    public static Logger getDefaultLogger() {
	if(logger == null)
	    logger = new Logger();
	return logger;
    }

    public void setLogLevel(int level) {
	this.logLevel = level;
    }

    public void error(String errorStr, Exception e) {
	out.println(formatString(errorStr));
	e.printStackTrace(out);
    }

    public void error(String errorStr) {
	out.println(formatString(errorStr));
    }

    public void info(String str) {
	if(logLevel > 0)
	    out.println(formatString(str));
    }

    public void info(String str, Exception e) {
	if(logLevel > 0)
	    out.println(formatString(str + ":" + e.getMessage()));
    }

    public void infooverride(String str) {
	out.println(formatString(str));
    }

    public void debug(String str) {
	if(logLevel == DEBUG) {
	    out.println(formatString(str));
	}
    }

    public void flush() {
	out.flush();
    }

    private String formatString(String str) {
	calendar.setTimeInMillis(System.currentTimeMillis());
	strbuilder.setLength(0);
	strbuilder.append(calendar.get(Calendar.HOUR));
	strbuilder.append(":");
	strbuilder.append(calendar.get(Calendar.MINUTE));
	strbuilder.append(":");
	strbuilder.append(calendar.get(Calendar.SECOND));
	strbuilder.append(",");
	strbuilder.append(calendar.get(Calendar.MILLISECOND));
	strbuilder.append(" ");
	strbuilder.append(str);
	return strbuilder.toString();
    }
}